<?php

namespace Biz\service\brand;

use Biz\cache\brand\BrandShowActivity;
use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\model\brandActivity\BlindBoxActivityPrize as mBlindBoxActivityPrize;
use Biz\model\brandActivity\BlindBoxChangeRecord as mBlindBoxChangeRecord;
use Biz\model\brandActivity\BlindBoxCueCard as mBlindBoxCueCard;
use Biz\model\brandActivity\BlindBoxLottery as mBlindBoxLottery;
use Biz\model\brandActivity\BrandActivityBasic;
use Biz\model\brandIndex\BrandMsg;
use Biz\pockets\constInterface\BlindBoxConfig;
use Biz\service\BrandIndex;
use Biz\tools\helper\Common;
use Doraemon\cache\UserCounter;
use Doraemon\service\Point;
use Doraemon\service\User;
use Doraemon\tools\BlockType;
use Doraemon\tools\Log;
use Doraemon\tools\PointConfig;
use Hhzbeads\Point\PointClient;

class BlindBoxLottery implements BlindBoxConfig
{
    /**
     * 打开盲盒
     * @param $uid
     * @param $boxId
     * @param $pageId
     * @return mixed
     * @throws \Exception
     */
    public static function openBox($uid, $boxId, $pageId = 0)
    {
        $source = $pageId > 0 ? self::SOURCE_H5_ACTIVITY : self::SOURCE_BLIND_BOX;
        $canOpenBoxNum = BlindBox::canOpenBox($uid, $source);
        if ($canOpenBoxNum <= 0) {
            throw new \Exception('今日开奖机会已用完，明天再来哦');
        }
        $hasOpened = mBlindBoxLottery::has(['uid' => $uid, 'box_id' => $boxId, 'page_id' => $pageId, 'addtime[>=]' => date('Y-m-d'), 'addtime[<]' => date('Y-m-d', strtotime('+1days'))]);
        if ($hasOpened) {
            throw new \Exception('该盲盒今天已经开过奖');
        }

        //用户是不是风控名单 todo 待提供名单

        $point = Point::getPoint($uid);
        if ($source == self::SOURCE_BLIND_BOX && $point < self::OPEN_BOX_POINT) {
            throw new \Exception('积分不足');
        }

        $prizeList = BlindBox::getPrizeList($uid, true);
        //获取中奖的奖品Id
        $lotteryPrizeId = self::getLotteryPrizeId($uid, $prizeList);
        if ($lotteryPrizeId > 0) {
            if (!BrandShowActivity::isLock($lotteryPrizeId . '-blind-box',2)) {
                $activityId = $prizeList[$lotteryPrizeId]['activity_id'];
                $masterActivityInfo = mBlindBoxActivity::getInfoByIdOfMaster($activityId);
                $prizeStock = $masterActivityInfo['prize_stock'];
                $prizeNum = $masterActivityInfo['prize_num'];
                $startDay = date('Ymd', $masterActivityInfo['start_time']);
                $today = date('Ymd');
                $endDay = date('Ymd', $masterActivityInfo['end_time']);
                $stock = $prizeStock;
                if ($endDay == $today && $prizeStock) {//最后一天
                    $stock = $prizeStock;
                } else if ($startDay == $today && $prizeStock <= 1 && $prizeNum > 1) {//第一天库存保证不少于1
                    $stock = 0;
                } else if ($prizeStock) {
                    //平均每天中奖，避免同一天中奖太多
                    $days = round(($masterActivityInfo['end_time'] - $masterActivityInfo['start_time']) / 86400);
                    $avgStock = ($days > 0) ? round($prizeNum / $days) : $prizeStock;
                    $lotteryNum = $prizeNum - $prizeStock;
                    $currentDays = round((time() - $masterActivityInfo['start_time']) / 86400);
                    if ($currentDays * $avgStock <= $lotteryNum) {
                        $stock = 0;
                    }
                }
                if (!$stock) {
                    $lotteryPrizeId = self::EMPTY_BOX_PRIZE_ID;
                }
                $masterActivityPrizeInfo = mBlindBoxActivityPrize::getInfoByIdOfMaster($lotteryPrizeId);
                switch ($masterActivityPrizeInfo['prize_type']) {
                    case mBlindBoxActivity::PRIZE_TYPE_JD_CARD:
                        if ($masterActivityPrizeInfo['prize_state'] != mBlindBoxActivityPrize::PRIZE_STATE_NORMAL) {
                            $lotteryPrizeId = self::EMPTY_BOX_PRIZE_ID;
                        }
                        break;
                }
            } else {
                $lotteryPrizeId = self::EMPTY_BOX_PRIZE_ID;
            }
        }
        $date = date('Ymd');
        if ($date < 20210227 && $lotteryPrizeId == self::EMPTY_BOX_PRIZE_ID) {
            //h5 活动页，如果有图片类型奖品，则返回图片类型奖品
            foreach ($prizeList as $prizeId => $row) {
                if ($row['prize_type'] == mBlindBoxActivityPrize::PRIZE_TYPE_LIMIT_CARD) {
                    $lotteryPrizeId = $prizeId;
                    break;
                }
            }
        }
        $newBoxId = BlindBox::getNewBoxId($uid, $boxId);
        $lotteryPrizeInfo = $prizeList[$lotteryPrizeId];

        //过滤内部员工及过期品牌中奖；品牌实物奖品，优惠券奖品，京东卡奖品
        $staffUids = Common::getStaffUids();
        $staffPrizeTypeArr = [
            mBlindBoxActivity::PRIZE_TYPE_GOODS,
            mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON,
            mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON,
            mBlindBoxActivity::PRIZE_TYPE_JD_CARD,
        ];
        if (in_array($uid, $staffUids) && in_array($lotteryPrizeInfo['prize_type'], $staffPrizeTypeArr)) {
            $lotteryPrizeId = self::EMPTY_BOX_PRIZE_ID;
            $lotteryPrizeInfo = $prizeList[$lotteryPrizeId];
        }

        $lotteryId = self::addLotteryData($uid, $boxId, $lotteryPrizeInfo, $newBoxId, $source, $pageId);
        $lotteryPrizeInfo['lottery_id'] = $lotteryId;
        $lotteryPrizeInfo['box_id'] = $newBoxId;
        $canOpenCount = BlindBox::canOpenBox($uid, $source);
        $lotteryPrizeInfo['can_open_count'] = $canOpenCount;

        if ($source == self::SOURCE_BLIND_BOX) {
            //扣除积分
            $request_id = $uid . '-' . $boxId . '-' . date('Ymd') . '-' . PointConfig::POINT_TYPE_BLIND_BOX_OPEN;
            $point = self::OPEN_BOX_POINT;
            PointClient::deductPoint($uid, $point, PointConfig::POINT_TYPE_BLIND_BOX_OPEN, $request_id, 'community', json_encode(['box_id' => $boxId, 'date' => date('Y-m-d')]));
        }

        self::afterOpenBox($uid, $lotteryId, $lotteryPrizeInfo);
        return $lotteryPrizeInfo;
    }

    private static function afterOpenBox($uid, $lotteryId, $lotteryPrizeInfo)
    {
        $activityId = $lotteryPrizeInfo['activity_id'];
        //中奖发布话题
        if (in_array($lotteryPrizeInfo['prize_type'], [mBlindBoxActivity::PRIZE_TYPE_GOODS, mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON,
            mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON, mBlindBoxActivity::PRIZE_TYPE_JD_CARD])) {//抽奖结果为实物、优惠券、JD 卡时，均自动生成一条动态发送，该动态可评论和点赞；
            BrandActivity::createDynamic($uid, $activityId, BrandActivityBasic::ACTIVITY_TYPE_BLIND_BOX);
            if (in_array($lotteryPrizeInfo['prize_type'], [mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON, mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON])) {
                $msg = '【超级盲盒】有住友抽中了 ' . $lotteryPrizeInfo['prize_title'] . '，点这里查看详情>"，点击后跳转至活动详情';
                BrandIndex::writeMsg($lotteryPrizeInfo['brand_id'], BrandMsg::MSG_TYPE_PASS, BrandMsg::MSG_SOURCE_BLIND_BOX, $msg, BlockType::TYPE_BLIND_BOX_LOTTERY, $activityId);
            }
        } else if ($lotteryPrizeInfo['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_POINT && $lotteryPrizeInfo['prize_value']) {
            //抽中积分
            $request_id = $uid . '-' . $lotteryId . '-' . PointConfig::POINT_TYPE_BLIND_BOX_LOTTERY;
            $point = $lotteryPrizeInfo['prize_value'];
            PointClient::awardPoint($uid, $point, PointConfig::POINT_TYPE_BLIND_BOX_LOTTERY, $request_id, 'community', json_encode(['lottery_id' => $lotteryId, 'date' => date('Y-m-d')]));
        }
    }

    public static function addLotteryData($uid, $boxId, $lotteryPrizeInfo, $newBoxId, $source, $pageId)
    {
        try {
            $lotteryId = mBlindBoxLottery::action(function () use ($uid, $boxId, $lotteryPrizeInfo, $newBoxId, $source, $pageId) {
                $lotteryPrizeId = $lotteryPrizeInfo['prize_id'];
                $prizeValue = isset($lotteryPrizeInfo['prize_value']) ? $lotteryPrizeInfo['prize_value'] : '';
                $expireTime = 0;
                switch ($lotteryPrizeInfo['prize_type']) {
                    case mBlindBoxActivity::PRIZE_TYPE_CUE_CARD:
                        $expireTime = date('Y-m-d', strtotime("+30 days"));
                        $expireTime = date('Y-m-d H:i:s', strtotime($expireTime) - 1);
                        $cueCardId = mBlindBoxCueCard::insertData(['uid' => $uid, 'card_type' => mBlindBoxCueCard::CARD_TYPE_EXCLUDE, 'from_source' => mBlindBoxCueCard::CARD_FROM_SOURCE_LOTTERY, 'expire_time' => $expireTime]);
                        if (empty($cueCardId)) {
                            Log::debug('add-lottery-data#add cue card fail#sql' . json_encode(mBlindBoxCueCard::log()));
                            return false;
                        }
                        $prizeValue = $cueCardId;
                        break;
                    case mBlindBoxActivity::PRIZE_TYPE_GOODS:
                    case mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON:
                    case mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON:
                    case mBlindBoxActivity::PRIZE_TYPE_JD_CARD:
                        $expireTime = date('Y-m-d', strtotime("+30 days"));
                        $expireTime = date('Y-m-d H:i:s', strtotime($expireTime) - 1);
                        break;
                }
                $lotteryData = [
                    'uid' => $uid,
                    'box_id' => $boxId,
                    'activity_id' => $lotteryPrizeInfo['activity_id'],
                    'brand_id' => isset($lotteryPrizeInfo['brand_id']) ? $lotteryPrizeInfo['brand_id'] : 0,
                    'prize_type' => $lotteryPrizeInfo['prize_type'],
                    'prize_id' => $lotteryPrizeInfo['prize_id'],
                    'prize_title' => $lotteryPrizeInfo['prize_title'],
                    'prize_value' => $prizeValue,
                    'prize_price' => isset($lotteryPrizeInfo['prize_price']) ? $lotteryPrizeInfo['prize_price'] : 0,
                    'prize_img' => isset($lotteryPrizeInfo['prize_img']) ? $lotteryPrizeInfo['prize_img'] : '',
                    'expire_time' => $expireTime,
                    'page_id' => $pageId
                ];
                $lotteryId = mBlindBoxLottery::insertData($lotteryData);
                if (!$lotteryId) {
                    Log::debug('add-lottery-data#add lottery fail#sql' . json_encode(mBlindBoxLottery::log()));
                    return false;
                }
                //更新活动的奖品状态为已领取
                if ($lotteryPrizeInfo['prize_type'] != mBlindBoxActivityPrize::PRIZE_TYPE_LIMIT_CARD) {//限定卡，不更新已领取状态
                    $res = mBlindBoxActivityPrize::updateData(['prize_state' => mBlindBoxActivityPrize::PRIZE_STATE_RECEIVED], ['id' => $lotteryPrizeId]);
                    if (!$res) {
                        Log::debug('add-lottery-data#update activity state fail#sql' . json_encode(mBlindBoxActivityPrize::log()));
                        return false;
                    }
                }
                if (!in_array($lotteryPrizeId, [self::EMPTY_BOX_PRIZE_ID, self::POINT_BOX_PRIZE_ID, self::CUE_CARD_BOX_PRIZE_ID])) {//减去奖品库存
                    $res = mBlindBoxActivity::decrPrizeStock($lotteryPrizeInfo['activity_id']);
                    if (!$res) {
                        Log::debug('add-lottery-data#update prize stock fail#sql' . json_encode(mBlindBoxActivity::log()));
                        return false;
                    }
                }
                //更换盲盒记录
                $res = mBlindBoxChangeRecord::insertData(['uid' => $uid, 'change_type' => mBlindBoxChangeRecord::CHANGE_TYPE_OPEN_BOX, 'box_id' => $boxId, 'new_box_id' => $newBoxId, 'source' => $source]);
                if (!$res) {
                    Log::debug('add-lottery-data#update box change record fail#sql' . json_encode(mBlindBoxChangeRecord::log()));
                    return false;
                }
                return $lotteryId;
            });
            if ($lotteryId) {
                return $lotteryId;
            } else {
                throw new \Exception('抽奖数据写入失败');
            }
        } catch (\Exception $e) {
            Log::debug('add-lottery-data-exception#msg:' . $e->getMessage() . '#code:' . $e->getCode() . '#file:' . $e->getFile() . '#line:' . $e->getLine() . '#lotteryPrizeInfo:' . $lotteryPrizeInfo);
            throw new \Exception('打开盲盒失败，请重试');
        }
    }

    public static function getLotteryListForDynamic($lotteryIds)
    {
        $result = [];
        $list = mBlindBoxLottery::selectByWhere(['id' => $lotteryIds]);
        $activityIds = array_column($list, 'activity_id');
        $activityIds = array_unique(array_filter($activityIds));
        $brandInfos = [];
        $activityList = [];
        if ($activityIds) {
            $activityList = mBlindBoxActivity::selectByWhere(['id' => $activityIds, 'brand_id[>]' => 0], ['brand_id', 'basic_id', 'shop_name']);
            $brandIds = array_column($activityList, 'brand_id');
            $activityList = array_column($activityList, null, 'id');
            $brandIds = $brandIds ? array_unique($brandIds) : [];
            $brandInfos = $brandIds ? Brand::getBrandListByIds4App($brandIds) : [];
        }
        foreach ($list as $row) {
            $activityInfo = ($row['activity_id'] && isset($activityList[$row['activity_id']])) ? $activityList[$row['activity_id']] : [];
            $brandInfo = ($row['brand_id'] && isset($brandInfos[$row['brand_id']])) ? $brandInfos[$row['brand_id']] : [];
            if ($row['brand_id']) {
                $logo = $brandInfo ? $brandInfo['logo'] : '';
                $name = $brandInfo ? $brandInfo['name'] : '';
            } else {
                $name = '好好住';
                $logo = 'https://img.haohaozhu.cn//Pic-imageShow//sq_270//0000097p?iv=1&v=4';
            }
            $prizeTitle = '';
            $prizeImg = $row['prize_img'];
            $desc = '由' . $name . '提供的¥' . $row['prize_price'] . '元';
            if ($row['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_GOODS) {
                $prizeTitle = $row['prize_title'];
                $desc .= $prizeTitle;
            } else if ($row['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_JD_CARD) {
                $prizeTitle = '京东卡';
                $prizeImg = 'https://img.haohaozhu.cn/Op-imageShow/4c21230ll0dn0Rd00qiwh4d?iv=1';
                $desc .= $prizeTitle;
            } else if ($row['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON) {
                $desc .= '优惠券';
            } else if ($row['prize_type'] == mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON) {
                $prizeImg = 'https://img.haohaozhu.cn/Op-imageShow/29ad220dw0dw0ge00qklcau?iv=1';
                $prizeTitle = ($activityInfo && $activityInfo[$row['activity_id']]) ? $activityInfo[$row['activity_id']]['shop_name'] : '';
                $desc .= '优惠券';
            }
            $result[$row['id']] = [
                'logo' => $logo,
                'name' => $name,
                'prize_id' => $row['prize_id'],
                'prize_price' => $row['prize_price'],
                'prize_title' => $prizeTitle,
                'prize_type' => 1,//1（实物奖品）2:全店通用券 3:单品优惠券 4：京东卡
                'prize_img' => $prizeImg,
                'desc' => $desc,
                'link' => BlindBox::getBlindBoxUrl()
            ];
        }
        return $result;
    }

    /**
     * 获取抽奖的奖品id
     * @param $uid
     * @param $prizeList
     * @return int|string
     */
    public static function getLotteryPrizeId($uid, $prizeList)
    {
        $prizeLotteryPriceArr = self::getPrizeLotteryPrice($prizeList);
        $userLevelId = self::getUserBoxLevel($uid);//查询用户的中奖级别
        //查询奖品的中奖权重
        $randMax = self::RAND_MAX;
        $prizeWeightArr = self::getPrizeWeight($uid, $prizeLotteryPriceArr, $userLevelId, $randMax);
        //根据权重获取中奖奖品id
        $lotteryPrizeId = self::getRandPrizeId($prizeWeightArr, $randMax);
        return $lotteryPrizeId;
    }

    public static function getRandPrizeId($prizeWeightArr, $randMax)
    {
        $resPrizeId = 0;
        foreach ($prizeWeightArr as $prizeId => $row) {
            if (!is_null($row['lottery_weight']) && $row['lottery_weight']) {
                $randNum = mt_rand(1, $randMax);
                $min = min($row['lottery_weight']);
                $max = max($row['lottery_weight']);
                if ($randNum >= $min && $randNum <= $max) {
                    $resPrizeId = $prizeId;
                    break;
                }
            }
        }
        return $resPrizeId;
    }

    private static function getUserReduceLevelRate($uid)
    {
        $now = date('Y-m-d H:i:s');
        $lastSixMonth = date('Y-m-d', strtotime("-6 month"));
        $lastThirdMonth = date('Y-m-d', strtotime("-3 month"));
        $lastOneMonth = date('Y-m-d', strtotime("-1 month"));
        $lastOneWeek = date("Y-m-d", strtotime("-1 week"));
        $where = [
            'uid' => $uid,
            'lottery_state[!]' => mBlindBoxLottery::LOTTERY_STATE_DEL,
            'addtime[>=]' => $lastSixMonth,
            'addtime[<=]' => $now,
            'prize_type[!]' => [mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON, mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON]
        ];
        $lotteryList = mBlindBoxLottery::selectByWhere($where, ['id', 'prize_type', 'prize_id', 'prize_price', 'prize_value', 'addtime']);
        $reduceBlindLevelRate = [];
        foreach ($lotteryList as $row) {
            $lotteryPrice = self::getLotteryPrice($row['prize_type'], $row['prize_price'], $row['prize_value']);
            if ($lotteryPrice >= self::PRIZE_BLIND_LEVEL['B']['price']['min']) {
                //中奖价值为A或B的奖品，6个月内A-H档中奖概率为0 优惠券除外
                $reduceBlindLevelRate['A'] = 0;
                $reduceBlindLevelRate['B'] = 0;
                $reduceBlindLevelRate['C'] = 0;
                $reduceBlindLevelRate['D'] = 0;
                $reduceBlindLevelRate['E'] = 0;
                $reduceBlindLevelRate['F'] = 0;
                $reduceBlindLevelRate['G'] = 0;
                $reduceBlindLevelRate['H'] = 0;
            } else if ($row['addtime'] >= $lastThirdMonth && $row['addtime'] < $now && $lotteryPrice >= self::PRIZE_BLIND_LEVEL['D']['price']['min']) {
                //中奖价值为C或D的奖品，3个月内A-H档中奖概率为0 优惠券除外
                $reduceBlindLevelRate['A'] = 0;
                $reduceBlindLevelRate['B'] = 0;
                $reduceBlindLevelRate['C'] = 0;
                $reduceBlindLevelRate['D'] = 0;
                $reduceBlindLevelRate['E'] = 0;
                $reduceBlindLevelRate['F'] = 0;
                $reduceBlindLevelRate['G'] = 0;
                $reduceBlindLevelRate['H'] = 0;
            } else if ($row['addtime'] >= $lastOneMonth && $row['addtime'] < $now && $lotteryPrice >= self::PRIZE_BLIND_LEVEL['G']['price']['min']) {
                //中奖价值为E、F、G、H的奖品。1个月内A-H档中奖概率为0 优惠券除外
                $reduceBlindLevelRate['A'] = 0;
                $reduceBlindLevelRate['B'] = 0;
                $reduceBlindLevelRate['C'] = 0;
                $reduceBlindLevelRate['D'] = 0;
                $reduceBlindLevelRate['E'] = 0;
                $reduceBlindLevelRate['F'] = 0;
                $reduceBlindLevelRate['G'] = 0;
                $reduceBlindLevelRate['H'] = 0;
            }
        }
        return $reduceBlindLevelRate;
    }

    /**
     * 获取当前活动中各个奖品中奖的权重
     * @param $uid
     * @param $prizeLotteryPriceArr
     * @param $userLevelId
     * @param $randMax
     * @return array
     */
    public static function getPrizeWeight($uid, $prizeLotteryPriceArr, $userLevelId, &$randMax)
    {
        $reduceBlindLevel = self::getUserReduceLevelRate($uid);
        if ($reduceBlindLevel) {//中奖概率为1/2的用户，从1～2000中抽取随机数
//            $randMax = $randMax * 2;
        }
        $randMax = $randMax * 1;
        $prizeWeightArr = [];
        foreach ($prizeLotteryPriceArr as $prizeId => $row) {
            $prizePrice = $row['prize_price'];
            $lotteryWeight = null;
            $lotteryLevel = null;
            foreach (self::PRIZE_BLIND_LEVEL as $level => $wightArr) {
                $lotteryLevel = $level;
                if ($wightArr['price']) {
                    if ($prizePrice >= $wightArr['price']['min'] && $prizePrice <= $wightArr['price']['max'] && !isset($reduceBlindLevel[$level])) {
                        $lotteryWeightKey = (isset($row['last_day']) && $row['last_day']) ? 'last_lottery_weight' : 'lottery_weight';
                        $lotteryWeight = isset($wightArr[$lotteryWeightKey][$userLevelId]) ? $wightArr[$lotteryWeightKey][$userLevelId] : null;
                        break;
                    }
                }
            }
            if (!is_null($lotteryWeight)) {
                $row['user_level_id'] = $userLevelId;
                $row['blind_level'] = $lotteryLevel;
                $row['lottery_weight'] = $lotteryWeight;
                $prizeWeightArr[$lotteryLevel][$prizeId] = $row;
            }
        }
        $result = [];
        foreach ($prizeWeightArr as $level => $prizeArr) {
            $index = array_rand($prizeArr);
            $result[$index] = $prizeArr[$index];
        }
        return $result;
    }

    public static function getPrizeLotteryPrice($prizeList)
    {
        $result = [];
        foreach ($prizeList as $prizeId => $row) {
            $prizePrice = isset($row['prize_price']) ? $row['prize_price'] : 0;
            $price = self::getLotteryPrice($row['prize_type'], $prizePrice, $row['prize_value']);
            $result[$prizeId] = [
                'prize_id' => $row['prize_id'],
                'prize_type' => $row['prize_type'],
                'prize_price' => $price,
                'activity_id' => $row['activity_id'],
                'last_day' => isset($row['last_day']) ? $row['last_day'] : false,
            ];
        }
        return $result;
    }

    private static function getLotteryPrice($prizeType, $pricePrice, $prizeValue)
    {
        $price = 0;
        switch ($prizeType) {
            case mBlindBoxActivity::PRIZE_TYPE_EMPTY_BOX:
                $price = 0;
                break;
            case mBlindBoxActivity::PRIZE_TYPE_GOODS:
                $price = $pricePrice;
                break;
            case mBlindBoxActivity::PRIZE_TYPE_SHOP_COUPON://优惠券价值：优惠券实际金额/2
                $price = bcdiv($pricePrice, 2, 2);
                break;
            case mBlindBoxActivity::PRIZE_TYPE_GOOD_COUPON://优惠券价值：优惠券实际金额/2
                $price = bcdiv($pricePrice, 2, 2);
                break;
            case mBlindBoxActivity::PRIZE_TYPE_JD_CARD://京东卡价值：实际金额*2
                $price = bcmul($pricePrice, 2, 2);
                break;
            case mBlindBoxActivity::PRIZE_TYPE_POINT://积分价值：10 积分 = 1 元
                $price = bcmul($prizeValue, 0.1, 2);
                break;
            case mBlindBoxActivity::PRIZE_TYPE_CUE_CARD://提示卡价值：排除卡 = 10 元
                $price = 10;
                break;
            case mBlindBoxActivity::PRIZE_TYPE_LIMIT_CARD://限定卡价值：限定卡 = 1 元
                $price = 1;
                break;
        }
        return $price;
    }

    public static function getUserBoxLevel($uid)
    {
        $userInfo = User::getUser($uid, true);
        $fans = UserCounter::getOneByType($uid, UserCounter::TYPE_FANS);
        $type = $userInfo['type'];
        $adminTypeInfo = $userInfo['admin_type_info'];
        $authTag = $adminTypeInfo['auth_tag'];
        $quality = $adminTypeInfo['quality'];
        $quality2 = $adminTypeInfo['quality2'];
        Log::info('#user-box-level#' . json_encode(['uid' => $uid, 'fans' => $fans, 'type' => $type, 'auth_tag' => $authTag, 'quality' => $quality, 'quality2' => $quality2]));
        $userLevelId = 0;
        foreach (self::USER_BLIND_LEVEL as $id => $row) {
            $fansFlag = false;
            $userTypeFlag = false;
            if ($row['fans']) {
                if ($fans >= $row['fans']['min'] && $fans <= $row['fans']['max']) {
                    $fansFlag = true;
                }
            } else {
                $fansFlag = true;
            }
            if ($row['user_type']) {
                if (in_array($type, $row['user_type']['type']) || in_array($authTag, $row['user_type']['auth_tag'])
                    || in_array($quality, $row['user_type']['quality']) || in_array($quality2, $row['user_type']['quality2'])) {
                    $userTypeFlag = true;
                }
            } else {
                $userTypeFlag = true;
            }
            if ($fansFlag && $userTypeFlag) {
                $userLevelId = $id;
                break;
            }
        }
        return $userLevelId;
    }
}